package com.sixth.util;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @ClassName EncryptionUtil
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/12 11:42
 **/
public class EncryptionUtil {

    // 使用MD5算法对密码进行加密存储
    public static String getMD5String2(String value) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");

            instance.update(value.getBytes());

            // digest为value经过哈希算法生成的16个字节的字节数组，也就是8个字符的字符串
            byte[] digest = instance.digest();

            // 将16个字节的字节数组转化为32位的hex值
            BigInteger bigInteger = new BigInteger(1, digest);

            return bigInteger.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


    // 对密码MD5加密，加盐

    public static String getMD5String(String password, String salt) {
        //加密方式
        String hashAlgorithmName = "MD5";
        //盐：为了即使相同的密码不同的盐加密后的结果也不同
        ByteSource byteSalt = ByteSource.Util.bytes(salt);
        //密码
        Object source = password;
        //加密次数
        int hashIterations = 2;
        SimpleHash result = new SimpleHash(hashAlgorithmName, source, byteSalt, hashIterations);
        return result.toString();
    }

}
